package com.qf.commons.mybatis.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * author Ken
 * create_time 2022/11/29
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 调用mybatis-plus的save方法时，会自动填充的属性
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("createTime", new Date());
        metaObject.setValue("updateTime", new Date());
        Object status = metaObject.getValue("status");
        if (status == null) {
            metaObject.setValue("status", 0);
        }
        metaObject.setValue("delFlag", 0);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", new Date());
    }
}
